home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / packer / tar / src / getopt.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  1KB  |  55 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  * get option letter from argument vector
  5.  */
  6. int    opterr = 1;        /* useless, never set or used */
  7. int    optind = 1;        /* index into parent argv vector */
  8. int    optopt;            /* character checked for validity */
  9. char    *optarg;        /* argument associated with option */
  10.  
  11. #define BADCH    (int)'?'
  12. #define EMSG    ""
  13. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  14.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  15.  
  16. getopt(nargc,nargv,ostr)
  17. int    nargc;
  18. char    **nargv,
  19.     *ostr;
  20. {
  21.     static char    *place = EMSG;    /* option letter processing */
  22.     register char    *oli;        /* option letter list index */
  23.     char    *index();
  24.  
  25. /*    fprintf(stderr, "getopt(): optind=%d optarg=0x%x\n", optind, optarg); */
  26.     if(!*place) {            /* update scanning pointer */
  27.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EO
  28. );
  29.         if (*place == '-') {    /* found "--" */
  30.             ++optind;
  31.             return(EOF);
  32.         }
  33.     }                /* option letter okay? */
  34.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  35.         if(!*place) ++optind;
  36.         tell(": illegal option -- ");
  37.     }
  38.     if (*++oli != ':') {        /* don't need argument */
  39.         optarg = NULL;
  40.         if (!*place) ++optind;
  41.     }
  42.     else {                /* need an argument */
  43.         if (*place) optarg = place;    /* no white space */
  44.         else if (nargc <= ++optind) {    /* no arg */
  45.             place = EMSG;
  46.             tell(": option requires an argument -- ");
  47.         }
  48.          else optarg = nargv[optind];    /* white space */
  49.         place = EMSG;
  50.         ++optind;
  51.     }
  52.     return(optopt);            /* dump back option letter */
  53. }
  54.  
  55.